local runtime = require 'jass.runtime'
local setmetatable = setmetatable
local table = table
local debug = debug

---static
ac.allacts = setmetatable({}, {__mode = 'kv'})

---事件派发
ac.notify = function (obj, name, ...)
    if not obj._events then return end
    local trigger = obj._events[name]
    if trigger then
        trigger:notify(...)
    end
end

---事件派发(有返回值)
ac.dispatch = function(obj, name, ...)
    if not obj._events then return end
    local trigger = obj._events[name]
    if trigger then
        return trigger:dispatch(...)
    end
end

---事件注册
ac.register = function(obj, name)
    if not obj._events then 
        obj._events = {}
    end 
    local trigger = obj._events[name]
    if not trigger then 
        trigger = ac.trigger.create(name)
        trigger.remove = function(t)
            for i = #t.actors, 1, -1 do 
                local actor = t.actors[i]
                actor:remove()
                ac.allacts[actor] = nil
            end 
            obj._events[name] = nil
        end
        obj._events[name] = trigger 
    end 
    return function (func)
        local actor = trigger:add_actor(func)
        ac.allacts[actor] = true
        return actor
    end
end
